package com.jwdf.uda.http;

import android.content.Context;
import android.text.TextUtils;

import com.jwdf.uda.BuildConfig;
import com.jwdf.uda.bean.PbBean;
import com.jwdf.uda.bean.UploadImageResponse;
import com.jwdf.uda.bean.UploadListImageResponse;
import com.jwdf.uda.bean.WXPayment;
import com.jwdf.uda.http.action.UploadImageAction;
import com.jwdf.uda.http.action.WXPayAction;
import com.jwdf.uda.utils.BroadcastUtils;
import com.jwdf.uda.utils.ResourceUtils;
import com.jwdf.uda.utils.ToastUtils;

/**
 * 类说明：处理信息
 */
public class HttpHelper {

    public static String showErrorMsg(Context context, int resultCode, PbBean.PbBaseServerInfo baseServerInfo, int defErrorResId) {
        return showErrorMsg(context, resultCode, baseServerInfo, ResourceUtils.getResourceString(context, defErrorResId));
    }

    public static String showErrorMsg(Context context, int resultCode, PbBean.PbBaseServerInfo baseServerInfo, String defError) {
        String msg = null;
        if (resultCode != AHttpService.IResCallback.RESULT_OK) {
            if (resultCode == AHttpService.IResCallback.RESULT_NET_ERROR) {
                msg = ResourceUtils.getResourceString(context, "error_net");
            } else if (resultCode == AHttpService.IResCallback.RESULT_NOT_NET_ERROR) {
                msg = ResourceUtils.getResourceString(context, "error_notnet");
            } else if (baseServerInfo != null && baseServerInfo.getCode() != 1) {
                if (baseServerInfo.getCode() == ServerErrorCode.USER_LOGOUT_FAILED.getCode()) {
                    BroadcastUtils.sendLoginInvalidBroadCast(context);
                }
                if (baseServerInfo.getCode() == ServerErrorCode.USER_REGISTER_MOBILE_OCCUPY.getCode()) {
                    BroadcastUtils.sendMobileOccupyBroadCast(context);
                }
                if (!TextUtils.isEmpty(baseServerInfo.getMsg())) {
                    msg = baseServerInfo.getMsg();
                } else {
                    msg = defError;
                }
            } else {
                msg = defError;
            }
        }
        return msg;
    }
    public static boolean showError(Context context, int resultCode, PbBean.PbBaseServerInfo baseServerInfo, int defErrorResId) {
        return showError(context, resultCode, baseServerInfo, ResourceUtils.getResourceString(context, defErrorResId));
    }
    public static boolean showError(Context context, int resultCode, PbBean.PbBaseServerInfo baseServerInfo, String defError) {
        String msg = showErrorMsg(context, resultCode, baseServerInfo, defError);
        if (msg == null) {
            return false;
        }
//        if(BuildConfig.DEBUG) {
            ToastUtils.show(context, msg);
//        }
        return true;
    }
    public static boolean showError(Context context, int resultCode, UploadImageResponse response, String defError) {
        String msg = showErrorMsg(context, resultCode, response, defError);
        if (msg == null) {
            return false;
        }
        if(BuildConfig.DEBUG) {
            ToastUtils.show(context, msg);
        }
        return true;
    }

    public static boolean showError(Context context, int resultCode, UploadListImageResponse response, String defError) {
        String msg = showErrorMsg(context, resultCode, response, defError);
        if (msg == null) {
            return false;
        }
//        ToastUtils.show(context, msg);
        return true;
    }

    public static String showErrorMsg(Context context, int resultCode, UploadImageResponse response, String defError) {
        String msg = null;
        if (resultCode != UploadImageAction.IResCallback.RESULT_OK) {
            if (resultCode == UploadImageAction.IResCallback.RESULT_FILE_NOTEXIT_ERROR) {
                msg = ResourceUtils.getResourceString(context, "error_file_notexit");
            } else if (resultCode == UploadImageAction.IResCallback.RESULT_UPIMAGE_ERROR) {
                msg = ResourceUtils.getResourceString(context, "error_image_error");
            } else if (resultCode == UploadImageAction.IResCallback.RESULT_NOT_NET_ERROR) {
                msg = ResourceUtils.getResourceString(context, "error_notnet");
            } else if (response != null && response.getStatus() != 1) {
                if (!TextUtils.isEmpty(response.getInfo())) {
                    msg = response.getInfo();
                } else {
                    msg = defError;
                }
            } else {
                msg = defError;
            }
        }
        return msg;
    }


    public static String showErrorMsg(Context context, int resultCode, UploadListImageResponse response, String defError) {
        String msg = null;
        if (resultCode != UploadImageAction.IResCallback.RESULT_OK) {
            if (resultCode == UploadImageAction.IResCallback.RESULT_FILE_NOTEXIT_ERROR) {
                msg = ResourceUtils.getResourceString(context, "error_file_notexit");
            } else if (resultCode == UploadImageAction.IResCallback.RESULT_UPIMAGE_ERROR) {
                msg = ResourceUtils.getResourceString(context, "error_image_error");
            } else if (resultCode == UploadImageAction.IResCallback.RESULT_NOT_NET_ERROR) {
                msg = ResourceUtils.getResourceString(context, "error_notnet");
            } else if (response != null && response.getStatus() != 1) {
                if (!TextUtils.isEmpty(response.getInfo())) {
                    msg = response.getInfo();
                } else {
                    msg = defError;
                }
            } else {
                msg = defError;
            }
        }
        return msg;
    }

    public static boolean showError(Context context, int resultCode, WXPayment response, String defError) {
        String msg = showErrorMsg(context, resultCode, response, defError);
        if (msg == null) {
            return false;
        }
        if(BuildConfig.DEBUG) {
            ToastUtils.show(context, msg);
        }
        return true;
    }
    public static String showErrorMsg(Context context, int resultCode, WXPayment response, String defError) {
        String msg = null;
        if (resultCode != WXPayAction.IResCallback.RESULT_OK) {
            if (resultCode == WXPayAction.IResCallback.RESULT_NET_ERROR) {
                msg = ResourceUtils.getResourceString(context, "error_net");
            } else if (resultCode == WXPayAction.IResCallback.RESULT_NOT_NET_ERROR) {
                msg = ResourceUtils.getResourceString(context, "error_notnet");
            } else if (response != null && response.getStatus() != 1) {
                if (!TextUtils.isEmpty(response.getResultCode())) {
                    msg = response.getResultCode();
                } else {
                    msg = defError;
                }
            } else {
                msg = defError;
            }
        }
        return msg;
    }
}
